package cn.fsd.service.impl;

import cn.fsd.domain.Commodity;
import cn.fsd.feignclients.CustomerFeignClient;
import cn.fsd.mapper.CommodityMapper;
import cn.fsd.query.delivery.PurchaseBean;
import cn.fsd.service.BusinessService;
import com.google.common.collect.Lists;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

@Service
public class BusinessServiceImpl implements BusinessService {

  @Resource
  CustomerFeignClient customerFeignClient;
  @Resource
  CommodityMapper commodityMapper;

  @Override
  public HashMap<Object,Object> getPurchaseList() {
    List<PurchaseBean> res = customerFeignClient.getPurchaseList();
    HashMap<Object,Object> result = new HashMap<>();
    Integer sum = res.stream()
        .mapToInt(PurchaseBean::getOrder_num) // 假设属性名为 value，根据实际情况修改
        .sum();
    result.put("order_num",sum);
    result.put("customer_num",customerFeignClient.getUserNum());
    //过滤非空
    res = res.parallelStream().filter(Objects::nonNull).collect(Collectors.toList());
    res.sort(Comparator.comparing(PurchaseBean::getPurchase_num));
    res = Lists.reverse(res);
    res = res.subList(0, 5);
    res = res.parallelStream().map(item -> {
      if (StringUtils.isEmpty(item.getCommodity_id())) {
        return item;
      }
      Commodity commodity = commodityMapper.selectByPrimaryKey(item.getCommodity_id());
      if (commodity != null) {
        item.setCommodity_name(commodity.getCommodity_name());
      }
      return item;
    }).collect(Collectors.toList());
    result.put("purchase_list",res);
    return result;
  }
}
